<!-- 弹窗 -->
jQuery(document).ready(function($) {
	
	//弹窗1
	$(".user_a").click(function(){
        $(".userinfo").show(300);
        return false;//关键是这里，阻止冒泡
    });
    $(".userinfo").click(function(){
        return false;
    });
	$(document).click(function(){
        $(".userinfo").hide(300);
    });
	//弹窗2
	$("#movenote").click(function() {
		$(".notebookOption").show(300);
		return false;
	});
	$(".notebookOption").click(function() {
		return false;
	});
	$(document).click(function() {
		$(".notebookOption").hide(300);
	});
	//弹窗3
	$("#newnotebook_a").click(function() {
		$(".newnotebook").show(300);
		return false;
	});
	$("#newnotebook").click(function() {
		$(".newnotebook").show(300);
		return false;
	});
	$(".newnotebook").click(function() {
		return false;
	});
	$(".cancel").click(function() {
		$(".newnotebook").hide(300);
	});
	//弹窗4
	$("#left_note").click(function() {
		$(".centre").show(300);
		return false;
	});
	$(".centre").click(function() {
		return false;
	});
	$("#left_newnote").click(function() {
		$(".centre").hide(300);
		if(select != null && select != "") {
			document.getElementById(select).className = "note";
			isFirst = true;
		}
		$("#right_Cid").attr("value", "0");
		//$("#right_Nid").attr("value", "");
		$("#right_Uid").attr("value", "");
		$("#title_input").val("");
		editor1.txt.clear();
	});
	
	$("#left_notebook").click(function() {
		$(".notebook").show(300);
		return false;
	});
	$(".notebook").click(function() {
		return false;
	});
	$(document).click(function() {
		$(".notebook").hide(300);
		document.getElementById("img4").src = "img/notebook.png";
	});
})

// 富文本编辑器
// var E = window.wangEditor
// var editor = new E('#editor')
// 或者  var editor = new E( document.getElementById('editor') )
// editor.create()

var E = window.wangEditor;
var editor1 = new E('#text_toolbar', '#text'); // 两个参数也可以传入 elem 对象，class 选择器

editor1.customConfig.menus = [ 
    //'head', // 标题
	'bold', // 粗体
	'fontSize', // 字号
	'fontName', // 字体
	'italic', // 斜体
	'underline', // 下划线
	'strikeThrough', // 删除线
	'foreColor', // 文字颜色
	//'backColor', // 背景颜色
	//'link', // 插入链接
	//'list', // 列表
	//'justify', // 对齐方式
	//'quote', // 引用
	//'emoticon', // 表情
	//'image', // 插入图片
	//'table', // 表格
	//'video', // 插入视频
	//'code', // 插入代码
	'undo', // 撤销
	'redo' // 重复
]
editor1.create();

function getContent() {
	var Cid = document.getElementById("right_Cid").value;
	var Nid = document.getElementById("right_Nid").value;
	var Uid = document.getElementById("right_Uid").value;
	var title = document.getElementById("title_input").value;
	//var content = editor1.txt.html();
	var content = editor1.txt.text();
	var mydate = new Date();
	var date = mydate.toLocaleDateString();
	
	if(title == "" || title == null) {
		title = "无标题";
	}
	//content = html_encode(content);
	var params = {
	        "id":Cid,
	        "Nid":Nid,
	        "Uid":Uid,
	        "title":title,
	        "content":content,
	        "date":date
	    };
	post("DoAddNote", params);
}

	
//发送POST请求跳转到指定页面
function post(URL, PARAMS) {
	var temp = document.createElement("form");
	temp.action = URL;
	temp.method = "post";
	temp.style.display = "none";

	for ( var x in PARAMS) {
		var opt = document.createElement("textarea");
		opt.name = x;
		opt.value = PARAMS[x];
		temp.appendChild(opt);
	}
	document.body.appendChild(temp);
	temp.submit();
	return temp;
}

function html_encode(str) {
	var s = "";
	if (str.length == 0)
		return "";
	s = str.replace(/&/g, "&gt1;");
	s = s.replace(/</g, "&lt;");
	s = s.replace(/>/g, "&gt;");
	s = s.replace(/ /g, "&nbsp;");
	s = s.replace(/\'/g, "&#39;");
	s = s.replace(/\"/g, "&quot;");
	s = s.replace(/\n/g, "<br>");
	return s;
}

function html_decode(str) {
	var s = "";
	if (str.length == 0)
		return "";
	s = str.replace(/&gt1;/g, "&");
	s = s.replace(/&lt;/g, "<");
	s = s.replace(/&gt;/g, ">");
	s = s.replace(/&nbsp;/g, " ");
	s = s.replace(/&#39;/g, "\'");
	s = s.replace(/&quot;/g, "\"");
	s = s.replace(/<br>/g, "\n");
	return s;
}

var msg = '';
if(msg == "登录成功" || msg == "提交成功" || msg == "创建成功" || msg == "删除成功") {
	$(function() {
    	toastr.success(msg, "success"); // Wire up an event handler to a button in the toast, if it exists
    })
} else if(msg == "提交失败" || msg == "创建失败" || msg == "删除失败") {
	$(function() {
    	toastr.error(msg, "error"); // Wire up an event handler to a button in the toast, if it exists
    })
} else if(msg == "该笔记本已存在，请换个名字") {
	$(function() {
		toastr.warning(msg, "error"); // Wire up an event handler to a button in the toast, if it exists
    })
}
//用于添加或者修改笔记后显示centre div
var Cid = '';
if(Cid != "" && Cid != null) {
	select = "note_id" + Cid;
	document.getElementById(select).className += " selected";
	$(".centre").show(300);
}
 
var isNotebookShow = "";
if(isNotebookShow) {
	$(".notebook").show();
}

document.getElementById("notebookName_img").src = "img/selected.png";

var selectedNid = '${selectedNid}';
var img1 = false;
var img2 = false;
var img3 = true;
var img4 = false;
function imgHover() {
	var e = event.srcElement;
	if(e.id == "img1") {
		document.getElementById("img1").src = "img/newnotes1.png";
	} else if(e.id == "img2")	 {
		document.getElementById("img2").src = "img/search1.png";
	} else if(e.id == "img3")	 {
		document.getElementById("img3").src = "img/note1.png";
	} else if(e.id == "img4")	 {
		document.getElementById("img4").src = "img/notebook1.png";
	} else if(e.id == "newnotebook_a" || e.id == "newnotebook_span" || e.id == "newnotebook_img") {
		document.getElementById("newnotebook_img").src = "img/newnotebook_hover.png";
	} else if(e.id == "notebookName"+selectedNid || e.id == "notebookName_span"+selectedNid || e.id == "notebookName_img"+selectedNid) {
		document.getElementById("notebookName_img"+selectedNid).src = "img/selected_hover.png";
	}
}
function imgUnhover() {
	var e = event.srcElement;
	if(e.id == "img1") {
		if(img1) {
			document.getElementById("img1").src = "img/newnotes2.png";
		}else {
			document.getElementById("img1").src = "img/newnotes.png";
		}
	} else if(e.id == "img2")	 {
		if(img2) {
			document.getElementById("img2").src = "img/search2.png";
		}else {
			document.getElementById("img2").src = "img/search.png";
		}
	} else if(e.id == "img3")	 {
		if(img3) {
			document.getElementById("img3").src = "img/note2.png";
		}else {
			document.getElementById("img3").src = "img/note.png";
		}
	} else if(e.id == "img4")	 {
		if(img4) {
			document.getElementById("img4").src = "img/notebook2.png";
		}else {
			document.getElementById("img4").src = "img/notebook.png";
		}
	} else if(e.id == "newnotebook_a" || e.id == "newnotebook_span" || e.id == "newnotebook_img") {
		document.getElementById("newnotebook_img").src = "img/newnotebook.png"
	} else if(e.id == "notebookName"+selectedNid || e.id == "notebookName_span"+selectedNid || e.id == "notebookName_img"+selectedNid) {
		document.getElementById("notebookName_img"+selectedNid).src = "img/selected.png";
	}
}
function imgReset() {
	img1 = img2 = img3 = img4 = false;
	document.getElementById("img1").src = "img/newnotes.png";
	document.getElementById("img2").src = "img/search.png";
	document.getElementById("img3").src = "img/note.png";
	document.getElementById("img4").src = "img/notebook.png";
}
function imgOnClick() {
	var e = event.srcElement;
	imgReset();
	if(e.id == "img1") {
		img1 = true;
		document.getElementById("img1").src = "img/newnotes2.png";
	} else if(e.id == "img2") {
		img2 = true;
		document.getElementById("img2").src = "img/search2.png";
	} else if(e.id == "img3") {
		img3 = true;
		document.getElementById("img3").src = "img/note2.png";
		$(".notebook").hide(300);
	} else if(e.id == "img4") {
		img4 = true;
		document.getElementById("img4").src = "img/notebook2.png";
	}
}
//点击笔记事件
var select = "";
var isFirst = true;
function onClick(title, Cid1, Nid) {
	var e = event.srcElement;
	
	//修改note div的class属性以改变其样式
	if(isFirst) {
		if(Cid != "" && Cid != null) {
			document.getElementById("note_id" + Cid).className = "note";
		}
		select = "note_id" + Cid1;
		document.getElementById(select).className += " selected";
		isFirst = false;
	} else {
		document.getElementById(select).className = "note";
		select = "note_id" + Cid1;
		document.getElementById(select).className += " selected";
	}
	
	$("#title_input").val(title);
	var cid = document.getElementById("Cid" + Cid1).value;
	var nid = document.getElementById("Nid" + Cid1).value;
	var uid = document.getElementById("Uid" + Cid1).value;;
	$("#right_Cid").attr("value", cid);
	$("#right_Nid").attr("value", nid);
	$("#right_Uid").attr("value", uid);
	
	document.getElementById("notebook_name").innerText = document.getElementById("notebookName_span"+nid).innerText;
	editor1.txt.clear();
	var content = html_decode(document.getElementById("content_span" + Cid1).innerText);
	editor1.txt.append('<p>' + content +'</p>');
}

//创建笔记本
function newNotebook() {
	var name = document.getElementById("newnotebook_input").value;
	var Uid = '${Uid}';
	
	var params = {
	        "Uid":Uid,
	        "name":name
	    };
	post("DoNewNotebook", params);
}

//选择笔记本
function selectNotebook(Nid) {
	var name = document.getElementById("newnotebook_input").value;
	var Uid = '';
	
	var params = {
			"Nid":Nid,
			"Uid":Uid
	}
	post("DoSelectNotebook", params);
}

//删除笔记
function deleteNote(Cid, Nid) {
	var Uid = '';
	var notebookMsg = '${notebookMsg}';
	
	var params = {
			"Cid":Cid,
			"Nid":Nid,
			"Uid":Uid,
			"notebookMsg":notebookMsg
	}
	post("DoDeleteNote", params);
}

//显示所有笔记
function toIndex() {
	if('${notebookMsg}' != "所有笔记") {
		var Uid = '';
		
		var params = {
				"Uid":Uid,
		}
		post("ToIndex", params);
	}
}

//删除笔记本
function delectNotebook(Nid) {
	var Uid = '${Uid}';
	var notebookMsg = '${notebookMsg}';
	var name = document.getElementById("notebookName_span"+ Nid).innerText;
	if(name == "默认") {
		$(function() {
			toastr.warning("你不能删除这个笔记本", "error"); // Wire up an event handler to a button in the toast, if it exists
        })
	} else {
		var params = {
				"Nid":Nid,
				"Uid":Uid,
				"notebookMsg":notebookMsg
		}
		post("DoDeleteNotebook", params);
	}
}
//移动笔记到其他笔记本
function changeNotebook(Nid) {
	$(".notebookOption").hide(300);
	document.getElementById("notebookName_img"+selectedNid).src = "";
	document.getElementById("notebookName_img" + Nid).src = "img/selected.png";
	selectedNid = Nid;
	$("#right_Nid").attr("value", Nid);
	document.getElementById("notebook_name").innerText = document.getElementById("notebookName_span"+Nid).innerText;
}
//搜索笔记本
function onInput(num) {
	if(num != 3) {
		var value = document.getElementById("findNoteBook" + num).value;				
			var name = "";
			if(num == 1) {
				if(value != "" && value != null) {
					if(name.indexOf(value) == -1) {
						$("#notebookName").hide();
					}else {
						$("#notebookName").show();
					}
				}else {
					$("#notebookName").show();
				}
			} else if(num == 2) {
				if(value != "" && value != null) {
					if(name.indexOf(value) == -1) {
						$("#noteBookName").hide();
						//document.getElementById("noteBookName").style.display="none";
					}else {
						$("#noteBookName").show();
					}
				}else {
					$("#noteBookName").show();
				}
			}
	}else {
		var noteValue = document.getElementById("findNote").value;				
			var content = "";
			var title = "";
			if(noteValue != "" && noteValue != null) {
				if(title.indexOf(noteValue) == -1 && content.indexOf(noteValue) == -1) {
					$("#note_id").hide();
					//document.getElementById("noteBookName").style.display="none";
				}else {
					$("#note_id").show();
				}
			}else {
				$("#note_id").show();
			}
		}
	}														